package projectpractice;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.List;

public abstract class DishesPracticeFileImpl implements DishesPracticeService {
    private static ObjectMapper objectMapper = new ObjectMapper();
    private static File file = new File("./data1.json");

    @Override
    public void init() {
        if (!file.exists()) {
            return;
        }
        try {
            List<Dishes> dishesPracticeList = objectMapper.readValue(file,
                    new TypeReference<List<Dishes>>() {
                    });
            for (Dishes dishesPractice : dishesPracticeList) {
                add(dishesPractice);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void store(List<Dishes> dishesPractices) {
        if (dishesPractices == null) {
            return;
        }
        try {
            objectMapper.writeValue(file, dishesPractices);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
